/**
 * Created by lixiaodi on 2018/9/27 0027.
 */

mini.parse();
var grid = mini.get("datagrid1");
var tree = mini.get("tree1");
//grid.load(ajaxUrl + "/DepartmentController/getList.do");
//grid.load();
var url = bootPATH + "../frame/frame1/pages/";
//加载树结构
$(function(){
    $.ajax({
        url : ajaxUrl + "/TreeController/getAllTree.do",
        type:"post",
        success: function (data) {
            tree.setData(data.data);
        },
        error:function(data){
            mini.alert("加载失败");
        }
    })
});
//点击树节点的时候显示
tree.on("nodeselect", function (e) {
    //被选中得节点
    var selecNode = e.node;
    //如果点击了节点
    if (selecNode) {
        ajaxBack(selecNode);
    } else {
        grid.setData([]);
        grid.setTotalCount(0);
    }
});

//增加
function add() {
    var selectNode = tree.getSelectedNode();
    //存放父节点id的对象
    var parNodeObj = {};
    //得到被选择节点的所有父节点
    if(selectNode.id.substring(0,2) == "de"){
        var parNode = tree.getAncestors(selectNode);
        console.log(parNode)
        //得到集团id和公司id
        parNodeObj.depId = selectNode.pk;
        for(var i = 0; i < parNode.length; i++){
            if(parNode[i].id.substring(0,2) == "gr"){
                parNodeObj.groupId = parNode[i].pk;
            }else if(parNode[i].id.substring(0,2) == "co"){
                parNodeObj.companyId = parNode[i].pk;
            }
        }
    }else if(selectNode.id.substring(0,2) == "co"){
        parNodeObj.groupId = selectNode.pid.replace(/[^0-9]/ig,"");
        parNodeObj.companyId = selectNode.pk;
        parNodeObj.depId = "";
    }else if(selectNode.id.substring(0,2) == "gr"){
        parNodeObj.groupId = selectNode.pk;
        parNodeObj.companyId = "";
        parNodeObj.depId = "";
    }
    console.log(parNodeObj);
    //点击打开
    mini.open({
        url: url +"alertBox/departmentWindow.html",
        title: "新增部门", width: 600, height: 400,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {
                action: "new",
                parsId : parNodeObj,
                parentText : selectNode.text
            };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
            ajaxBack(selectNode);
        }
    });
}
//编辑
function edit() {
    var row = grid.getSelected();
    var selectNode = tree.getSelectedNode();
    if (row) {
        mini.open({
            url: url +"alertBox/departmentWindow.html",
            title: "编辑部门", width: 600, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", id: row.departmentId };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                ajaxBack(selectNode);
            }
        });

    } else {
        mini.alert("请选中一条记录");
    }

}
//删除
function remove() {
    var selectNode = tree.getSelectedNode();
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        mini.confirm("确定删除选中记录？","系统提示",function(action){
            if (action == "ok") {
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.departmentId);
                }
                var id = ids.join(',');
                grid.loading("操作中，请稍后......");
                $.ajax({
                    url: ajaxUrl + "/DepartmentController/deletes.do?ids=" + id,
                    type:"post",
                    success: function (text) {
                        ajaxBack(selectNode);
                    },
                    error: function () {
                        mini.alert('失败')
                    }
                });
            }
        })
    } else {
        mini.alert("请选中一条记录");
    }
}
//查询
function search() {
    var key = mini.get("key").getValue();
    $.ajax({
        url: ajaxUrl + "/DepartmentController/getListByParam.do",
        type:"post",
        datatype:"json",
        data:{
            departmentName:key
        },
        success: function (data) {
            grid.setData(data.data)
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
function onKeyEnter(e) {
    search();
}
//点击树得到数据表
function ajaxBack(selecNode){
    $.ajax({
        url : ajaxUrl + "/DepartmentController/getlistByPid.do",
        type : "POST",
        data : {
            id : selecNode.pk,
            types : selecNode.id.substring(0,2)
        },
        success : function(data){
            if(data.data){
                grid.setData(data.data);
            }else{
                showTips(data.msg);
                grid.setData(data.data);
            }
        },
        error : function(data){
            mini.alert(data.msg);
        }
    })
}
/////////////////////////////////////////////////
function onBirthdayRenderer(e) {
    var value = e.value;
    if (value) return mini.formatDate(value, 'yyyy-MM-dd');
    return "";
}